package com.weilan.ark3.buffer;

import com.weilan.ark3.entity.ControlDto;
import com.weilan.ark3.entity.ControlRtu;
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * //com6:读取AC数据队列
 * //com3:读取com2的队列
 * //controlQueue：存放控制AC的控制指令
 * //controlPVQueue:存放控制光伏的指令
 */
@Component
public class MsgQueue {
    //定义最大数的值
    private static Integer MAX_SIZE = 200;

    //com1
    private static Queue<ControlRtu> com6queue;
    //com2消息
    private static Queue<ControlRtu> com3queue;
    private static Queue<ControlRtu> com2queue;
    //com1控制
    private static Queue<ControlDto> controlQueue;
    //com2控制
    public static Queue<ControlDto> controlPVQueue;

    public static Deque<ControlDto> controlDeque;

    /*
     * 定义一个TCP队列
     * */
    public static Map<Integer, Queue<ControlRtu>> tcpQueueMap;//tcp队列集合
//    public static  Queue<ControlRtu> tcpQueue;//某一个队列


//    /*
//     * 定义第二个TCP队列
//     * */
//    public static Queue<ControlRtu> tcp2Queue;
//
//    /*
//     * 定义第三个TCP队列
//     * */
//    public static Queue<ControlRtu> tcp3Queue;
//    /*
//     * 定义第四个TCP队列
//     * */
//    public static Queue<ControlRtu> tcp4Queue;

    public static boolean writeToQueue(ControlRtu data) {
        if (data.getComName().equalsIgnoreCase("COM1")) {
            //判断通讯中断对插入报文的影响
            if (instanceCom6Queue().size() < MAX_SIZE) {
                instanceCom6Queue().add(data);
//                System.out.println("插入数据" + data);
            }
        } else if (data.getComName().equalsIgnoreCase("COM2")) {
            if (instanceCom2Queue().size() < MAX_SIZE) {
                instanceCom2Queue().add(data);
                //System.out.println("插入数据"+data);
            }
        } else if (data.getComName().equalsIgnoreCase("COM3")) {
            if (instanceCom3Queue().size() < MAX_SIZE) {
                instanceCom3Queue().add(data);
                //System.out.println("插入数据"+data);
            }
        } else if (data.getComName().equalsIgnoreCase("TCP")) {
            Integer slaveId = data.getSlaveId();
            if(MsgQueue.tcpQueueMap == null){
                tcpQueueMap = new HashMap<>();
                MsgQueue.tcpQueueMap.put(slaveId,new LinkedBlockingQueue<>());
            }
            Set<Integer> tcpIdSet = DataArray.slaveDataMap.keySet();
            for (Integer key : tcpIdSet) {
                if (slaveId.equals(key)) {
                    if (instanceTCPQueue(slaveId).size() < MAX_SIZE) {
                        instanceTCPQueue(slaveId).add(data);
                    }
                }
            }
        }
        return true;
    }


    public static void writeToControlTcp(ControlDto data) {
        if (instanceControlQueue().size() < MAX_SIZE) {
            instanceControlQueue().add(data);
        }
    }

    public static void writeToControlPV(ControlDto data) {
        if (instanceControlPVQueue().size() < MAX_SIZE) {
            instanceControlPVQueue().add(data);
        }
    }

    public static ControlRtu readFromCom6Queue() {
        ControlRtu data = instanceCom6Queue().poll();
        instanceCom6Queue().add(data);
        return data;
    }

    public static ControlDto readFromControlQueue() {
        ControlDto data = instanceControlQueue().poll();
        return data;
    }

    public static ControlRtu readFromCom3Queue() {
        ControlRtu data = instanceCom3Queue().poll();
        instanceCom3Queue().add(data);
        return data;
    }


    public static ControlRtu readFromCom2Queue() {
        ControlRtu data = instanceCom2Queue().poll();
        instanceCom2Queue().add(data);
        return data;
    }

    public static ControlDto readFromPVQueue() {
        ControlDto data = instanceControlPVQueue().poll();
        return data;
    }

    /*
     * 从双端队列中读取
     * */
    public static ControlDto readFromDueue() {
        ControlDto data = instanceControlDeue().getFirst();
        return data;
    }


    /**
     * 读取TCP队列
     */
    public static ControlRtu readFromTCPQueue(Integer slaveId) {
        ControlRtu data = instanceTCPQueue(slaveId).poll();
        instanceTCPQueue(slaveId).add(data);
        return data;
    }

    //
//    /*
//     * 读取TCP2队列
//     * */
//
//    public static ControlRtu readFromTCP2Queue() {
//        ControlRtu data = instanceTCP2Queue().poll();
//        return data;
//    }
//
//    /*
//     * 读取TCP3队列
//     * */
//    public static ControlRtu readFromTCP3Queue() {
//        ControlRtu data = instanceTCP3Queue().poll();
//        return data;
//    }
//    /*
//     * 读取TCP3队列
//     * */
//    public static ControlRtu readFromTCP4Queue() {
//        ControlRtu data = instanceTCP4Queue().poll();
//        return data;
//    }
    //初始化
    private static synchronized Queue<ControlRtu> instanceCom3Queue() {
        if (com3queue == null) {
            com3queue = new LinkedBlockingQueue<ControlRtu>(MAX_SIZE);
        }
        return com3queue;
    }

    private static synchronized Queue<ControlRtu> instanceCom2Queue() {
        if (com2queue == null) {
            com2queue = new LinkedBlockingQueue<ControlRtu>(MAX_SIZE);
        }
        return com2queue;
    }

    //初始化
    private static synchronized Queue<ControlRtu> instanceCom6Queue() {
        if (com6queue == null) {
            com6queue = new LinkedBlockingQueue<ControlRtu>(MAX_SIZE);
        }
        return com6queue;
    }

    //初始化
    private static synchronized Queue<ControlDto> instanceControlQueue() {
        if (controlQueue == null) {
            controlQueue = new LinkedBlockingQueue<ControlDto>(MAX_SIZE);
        }
        return controlQueue;
    }

    //初始化光伏控制队列
    private static synchronized Queue<ControlDto> instanceControlPVQueue() {
        if (controlPVQueue == null) {
            controlPVQueue = new LinkedBlockingQueue<ControlDto>(MAX_SIZE);
        }
        return controlPVQueue;
    }

    /*
     *初始化双端队列
     * */
    private static synchronized Deque<ControlDto> instanceControlDeue() {
        if (controlDeque == null) {
            controlDeque = new LinkedList<ControlDto>();
        }
        return controlDeque;
    }


    /*
     * 初始化TCP队列
     * */
    private static synchronized Queue<ControlRtu> instanceTCPQueue(Integer slaveId) {
        Queue<ControlRtu> tcpQueue = tcpQueueMap.get(slaveId);
        if (tcpQueue == null) {
            tcpQueue = new LinkedBlockingQueue<ControlRtu>(MAX_SIZE);
            tcpQueueMap.put(slaveId,tcpQueue);
        }
        return tcpQueue;
    }

    //    /*
//     * 初始化TCP队列2
//     * */
//    private static synchronized Queue<ControlRtu> instanceTCP2Queue() {
//        if (tcp2Queue == null) {
//            tcp2Queue = new LinkedBlockingQueue<ControlRtu>(MAX_SIZE);
//        }
//        return tcp2Queue;
//    }
//
//
//    /*
//     * 初始化TCP队列3
//     * */
//
//    private static synchronized Queue<ControlRtu> instanceTCP3Queue() {
//        if (tcp3Queue == null) {
//            tcp3Queue = new LinkedBlockingQueue<ControlRtu>(MAX_SIZE);
//        }
//        return tcp3Queue;
//    }
//    /*
//     * 初始化TCP队列4
//     * */
//    private static synchronized Queue<ControlRtu> instanceTCP4Queue() {
//        if (tcp4Queue == null) {
//            tcp4Queue = new LinkedBlockingQueue<ControlRtu>(MAX_SIZE);
//        }
//        return tcp4Queue;
//    }
    public Integer getMaxSize() {
        return MAX_SIZE;
    }

    /*
     * 删除队列中的数据
     * */
    public static void deleteDueue() {
        instanceControlDeue().removeFirst();
    }

    /*
     * 获取队列的长度
     * */
    public static int getDueuSize() {
        int size = instanceControlDeue().size();
        return size;
    }

    public static Integer com3QueueSize() {
        return instanceCom3Queue().size();
    }

    public static Integer com2QueueSize() {
        return instanceCom2Queue().size();
    }

    public static Integer com6QueueSize() {
        return instanceCom6Queue().size();
    }

    public static Integer controlQueueSize() {
        return instanceControlQueue().size();
    }

    public static Integer controlQueuePVSize() {
        return instanceControlPVQueue().size();
    }

    public static Integer controlTCPSize(Integer slaveId) {
        return instanceTCPQueue(slaveId).size();
    }
}
